package com.example.task1;

import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.example.task1.application.MyApplication;
import com.example.task1.ui.fastfood.FastFoodFragment;
import com.example.task1.ui.home.HomeFragment;
import com.example.task1.ui.news.NewsFragment;
import com.example.task1.ui.poverty.PovertyFragment;
import com.example.task1.ui.service.ServiceFragment;
import com.example.task1.ui.user.UserFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNav;
    private FragmentManager mManager;
    private ActionBar mSupportActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSupportActionBar = getSupportActionBar();
        mSupportActionBar.setTitle("首页");
        initView();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mManager = getSupportFragmentManager();
        mManager.beginTransaction().add(R.id.fragment, new HomeFragment()).commit();
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.home:
                    mManager.beginTransaction().replace(R.id.fragment,new HomeFragment()).commit();
                    mSupportActionBar.setTitle("首页");
                    break;
                case R.id.service:
                    mManager.beginTransaction().replace(R.id.fragment,new ServiceFragment()).commit();
                    mSupportActionBar.setTitle("全部服务");
                    break;
                case R.id.fast:
                    boolean logins = MyApplication.isLogin(this);
                    if (logins) {
                        break;
                    }
                    mManager.beginTransaction().replace(R.id.fragment,new FastFoodFragment()).commit();
                    mSupportActionBar.setTitle("厨房助手");
                    break;
                case R.id.news:
                    mManager.beginTransaction().replace(R.id.fragment,new NewsFragment()).commit();
                    mSupportActionBar.setTitle("新闻资讯");
                    break;
                case R.id.user:
                    boolean login = MyApplication.isLogin(this);
                    if (login) {
                        break;
                    }
                    mSupportActionBar.setTitle("个人中心");
                    mManager.beginTransaction().replace(R.id.fragment,new UserFragment()).commit();
                    break;
            }
            return true;
        });

    }

    private void initView() {
        bottomNav = findViewById(R.id.bottom_nav);
    }
}